package org.firefly.db.dialect;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 方言的默认抽象实现类
 * @author firefly
 *
 */
public abstract class DialectImpl implements IDialect{
	protected final static SimpleDateFormat dateSDF = new SimpleDateFormat("yyyy-MM-dd");
	protected final static SimpleDateFormat dateTimeSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 数据库支持的类型
	 * @return
	 */
	protected abstract Map<Integer,String> getTypes();
	
	@Override
	public String javaType2SQLType(int javaType){
		String type = getTypes().get(javaType);
		if(type==null){
			
			System.err.println("数据库不支持此类型，[java.sql.Types代码："+javaType+"]");
		}
		return type;
	}
	
	@Override
	public int getLength(int javaType, int length){
	    return length;
	}
	
	@Override
	public int sqlType2JavaType(String sqlType){
		Iterator<Entry<Integer,String>> it = getTypes().entrySet().iterator();
		Integer type =null;
		while (it.hasNext()) {
			Map.Entry<Integer,String> entry = it.next();
			Object obj = entry.getValue();
			if (obj.equals(sqlType)) {
				type = entry.getKey();
				break;
			}
		}
		if(type==null){
			System.err.println("数据库不支持此类型，"+sqlType+"]");
		}
		return type;
	}
	
	/**
	 * 字符串处理，增加转义符
	 * @param a
	 * @return
	 */
	public String processStr(String a){
		if(a==null)return "";
		return "'"+a.replace("'", "''")+"'";
	}
	

	@Override
	public int maxCols() {
		return -1;
	}
}
